import jwt
import time
from django.conf import settings
from django.http import JsonResponse

# 生成token
def generate_token(data):
  '''
   data: 要加密的数据,数据类型是字典
   '''
  # 设置数据的过期时间
  data.update({'exp': time.time()+settings.TOOKEN_EXPIRE})
  # 数据的加密
  token = jwt.encode(data, settings.LOGIN_TOKEN_KEY, algorithm='HS256')
  return token

# 验证token
def verify_token(token):
  # try:
  payload = jwt.decode(token, settings.LOGIN_TOKEN_KEY, algorithms=['HS256'])
  # except Exception as e:
  # return JsonResponse({"data": False, "msg": "token已失效"}, status=200)
  # else:
  return payload


if __name__ == '__main__':
    print(generate_token({'username': 'admin'}))